import pandas as pd
from cnsenti import Sentiment, Emotion
import bhsenti


def emotion_analysis_bhsenti():
    # 读取 Excel 文件
    file_path = "../public/data/tables/data_orin.xlsx"  # 替换为你的文件路径
    df = pd.read_excel(file_path)

    # 初始化 Sentiment 和 Emotion 实例
    senti = Sentiment()
    emotion = Emotion()

    # 创建用于存储结果的列
    df['words'] = 0
    df['sentences'] = 0
    df['pos'] = 0.0
    df['mid'] = 0.0
    df['neg'] = 0.0
    df['好'] = 0
    df['乐'] = 0
    df['哀'] = 0
    df['怒'] = 0
    df['惧'] = 0
    df['恶'] = 0
    df['惊'] = 0

    # 遍历每一行，进行情感分析
    for i, row in df.iterrows():
        text = row['text']

        # 进行情感词分析
        sentiment_result = senti.sentiment_count(text)
        df.at[i, 'words'] = sentiment_result['words']
        df.at[i, 'sentences'] = sentiment_result['sentences']

        # 使用 bhsenti 进行情感分析
        pre_info = bhsenti.predict_info(text)
        scores = pre_info['score']
        df.at[i, 'neg'] = scores[0]
        df.at[i, 'mid'] = scores[1]
        df.at[i, 'pos'] = scores[2]

        # 进行情绪词分析
        emotion_result = emotion.emotion_count(text)
        df.at[i, '好'] = emotion_result['好']
        df.at[i, '乐'] = emotion_result['乐']
        df.at[i, '哀'] = emotion_result['哀']
        df.at[i, '怒'] = emotion_result['怒']
        df.at[i, '惧'] = emotion_result['惧']
        df.at[i, '恶'] = emotion_result['恶']
        df.at[i, '惊'] = emotion_result['惊']

    # 添加自增的 "id" 列
    df['id'] = range(1, len(df) + 1)

    # 将结果保存回 Excel 文件
    output_file_path = "../public/data/tables/data_emotion_analysis.xlsx"  # 替换为你的输出文件路径
    df.to_excel(output_file_path, index=False)


if __name__ == '__main__':
    emotion_analysis_bhsenti()
